var ns, toast, gdMap;
var app = new Vue({
    el: '#app',
    data: {
        form: {
            PhoneNo: '',
            action: 'saveuserphone'
        },
        iskaoqin: false,//是否打卡
        GPSLocation: '正在定位中...',
        datalon:'',//经度
        datalat:'',//纬度
        minDistance:0,//最小距离
        maxDistance:0,//最大距离
        isArea:false,//是否在打卡区范围内
        nowTime: '',
        setupData: {},
        onWork: {},
        offWork: {},
        companyName: '',//公司名字
        postionName: '',// 部门
        userHeadImg: '',// 头像
        userName: '',// 用户名
        dakamsg: {
            dakaType: '',
            dakaTimer: '',
            dakaStatus: '',
        },

    },
    methods: {
        get_data: function () {
            var that = this;
            var options = {};
            ns.post(options, function (succeed, data, err) {
                console.log(JSON.stringify(data));
                if (succeed) {
                    that.setupData = data.setupData;
                    that.onWork = data.onWork;
                    that.offWork = data.offWork;
                    var test=data.setupData.workGps;
                    var i = test.lastIndexOf(',');
                    var j = test.length;
                    that.datalon= test.slice(0 , i );
                    that.datalat= test.slice(i + 1, j);
                    that.minDistance=data.setupData.minDistance;
                    that.maxDistance=data.setupData.maxDistance;
                    // that.datalon=
                    that.get_yinsi();
                } else {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                command: 'App/CheckOn/GetCheckOnStatus'
            })
        },
        get_user_data: function () {
            var that = this;
            // 公司名称
            var companyName = api.getPrefs({
                sync: true,
                key: 'companyName'
            });
            // 岗位名称
            var positionname = api.getPrefs({
                sync: true,
                key: 'positionname'
            });
            // 用户名称
            var username = api.getPrefs({
                sync: true,
                key: 'username'
            });
            // 用户头像
            var headimg = api.getPrefs({
                sync: true,
                key: 'headimg'
            });
            that.companyName = companyName || '重庆永友网络科技有限公司';
            that.postionName = positionname ? positionname : "";
            that.userHeadImg = headimg || '../image/default.png';
            // that.userHeadImg =  '../image/default.png';
            that.userName = username || '匿名用户';
        },
        //打卡
        do_daka: function (type) {
            var that = this;
            if(!that.isArea){
                api.toast({
                    msg: '不在打开区内',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            var options = {};
            options.location = that.GPSLocation;
            if (type == 1) {
                ns.post(options, function (succeed, data, err) {

                    if (succeed) {
                        that.iskaoqin = true;
                        that.get_data();
                        setTimeout(() => {
                            that.dakamsg.dakaType = '上班';
                            that.dakamsg.dakaTimer = that.onWork.checkTime;
                            that.dakamsg.dakaStatus = that.onWork.checkStatusDesc;
                        }, 500);

                        // that.setupData=data.setupData;
                        // that.onWork=data.onWork;
                        // that.offWork=data.offWork;
                        // that.getLocationLocal();
                    } else {
                        api.toast({
                            msg: err,
                            duration: 2000,
                            location: 'bottom'
                        });
                    }
                }, {
                    command: 'App/CheckOn/CheckOn'
                })

            }
            if (type == 2) {
                ns.post(options, function (succeed, data, err) {

                    if (succeed) {
                        that.iskaoqin = true;
                        that.get_data();
                        setTimeout(() => {
                            that.dakamsg.dakaType = '下班';
                            that.dakamsg.dakaTimer = that.offWork.checkTime;
                            that.dakamsg.dakaStatus = that.offWork.checkStatusDesc;
                        }, 500);

                    } else {
                        api.toast({
                            msg: err,
                            duration: 2000,
                            location: 'bottom'
                        });
                    }
                }, {
                    command: 'App/CheckOn/CheckOff'
                })
            }

            // that.iskaoqin=true;
            // that.getLocation();
        },
        //打开日历
        do_opentimer: function () {
            ns.openWin('kaoqin_rili', '考勤');
        },
        close_succeed: function () {
            var that = this;
            that.iskaoqin = false;
            that.get_data();
        },
        getCount(day) {
            var that = this;
            var myDate = new Date(day.date);
            if (!myDate) {
                return 0;
            }
            var y = myDate.getFullYear();
            var m = myDate.getMonth() + 1;
            var d = myDate.getDate();
            var totalCount = 0;

            that.list.forEach(function (item) {
                var lsDate = new Date(item.DateTime);
                var lsy = lsDate.getFullYear();
                var lsm = lsDate.getMonth() + 1;
                var lsd = lsDate.getDate();
                if (y == lsy && m == lsm && d == lsd) {
                    totalCount = item.count;
                    return false;
                }
            })
            return totalCount;
        },
        onConfirm(day) {
            var that = this;
            var y = day.getFullYear();
            var m = day.getMonth() + 1;
            var d = day.getDate();
            var nowdate = y + '年' + (m >= 10 ? m : '0' + m) + '月' + (d >= 10 ? d : '0' + d) + '日';
            that.showCalender = false;
            that.choosedatetime = nowdate;
            that.getdata();
        },

        // StartBmap.: function() {
        //     var that = this;
        //     gdMap.open(function(ret){
        //         if(ret.status){
        //             that.getLocation();
        //         }
        //     })
        // },
        // getLocation:function(){
        //     console.log("ddddddddddd");
        //     gdMap.getLocation({
        //         autoStop: true
        //     },function(ret,err){
        //         console.log("11111111111");
        //         console.log(JSON.stringify(ret));
        //     });
        // },

        // gdMap = new AMap.Map('container', {
        //     resizeEnable: false,
        //     center: [116.486409, 39.921489],
        //     zoom: 12
        // });
        // var citysearch = new AMap.CitySearch();

        // citysearch.getLocalCity(function (status, result) {
        //     if (status === 'complete' && result.info === 'OK') {
        //         if (result && result.city && result.bounds) {
        //             that.cityName = result.city;
        //             that.getLiveWeather();
        //         }
        //     }
        // });
        open: function () {
            var that = this;
            gdMap.updateMapViewPrivacy({
                privacyAgree: 'didAgree',
                privacyShow: 'didShow',
                containStatus: 'didContain'
            });
            gdMap.updateSearchPrivacy({
                privacyAgree: 'didAgree',
                privacyShow: 'didShow',
                containStatus: 'didContain'
            });
            gdMap.open({
                rect: {
                    x: 0,
                    y: 200,
                    w: 320,
                    h: 300
                },
                showUserLocation: true,
                zoomLevel: 11,
                center: {
                    lon: 116.4021310000,
                    lat: 39.9994480000
                },
                fixedOn: api.frameName,
                fixed: true,
            }, function (ret) {
                if (ret.status) {

                    that.getLocationLocal();
                } else {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });
        },
        reload_address: function () {
            var that = this;
            that.GPSLocation = '正在定位中...';
            that.get_yinsi();
        },
        get_yinsi: function () {
            var that = this;
            ns.confirmPer('location', function () {
                gdMap.updateMapViewPrivacy({
                    privacyAgree: 'didAgree',
                    privacyShow: 'didShow',
                    containStatus: 'didContain'
                });
                gdMap.updateSearchPrivacy({
                    privacyAgree: 'didAgree',
                    privacyShow: 'didShow',
                    containStatus: 'didContain'
                });
                that.getLocationLocal();
            })


        },
        getLocationLocal: function () {
            var that = this;
            var gdMap = api.require('aMap');
            gdMap.getLocation({
                autoStop: true,
            }, function (ret, err) {
                console.log(JSON.stringify(ret));
                if (ret.status) {
                    lat = ret.lat;
                    lon = ret.lon;
                    that.getNameFromCoords(lat, lon);
                }
                else {
                    that.GPSLocation = '定位获取失败..';
                    that.isArea=false;
                    api.toast({
                        msg: '请打开手机定位',
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });
        },
        getNameFromCoords: function (lat, lon) {
            var that = this;
            gdMap.getNameFromCoords({
                lon: lon,
                lat: lat
            }, function (ret, err) {
                if (ret.status) {
                    that.GPSLocation = ret.address;
                    that.getrangenum(lat,lon);
                } else {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });
        },
        getrangenum: function (lat,lon) {
            var that = this;
            gdMap.getDistance({
                start: {
                    lon: lon,
                    lat: lat
                },
                end: {
                    lon: that.datalon,
                    lat: that.datalat
                }
            }, function (ret, err) {
                if (ret.status) {
                    if(ret.distance>=that.minDistance&&ret.distance<=that.maxDistance){
                        that.isArea=true;
                    }else{
                        that.isArea=false;
                    }
                } else {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });
        },




        getTime: function () {
            var that = this;
            that.nowTime = moment().format('HH:mm:ss')
        },



    }
});

apiready = function () {
    ns = window.Foresight.Util;
    toast = new auiToast();
    // ns.init_pull();
    app.nowTime = moment().format('HH:mm:ss');
    gdMap = api.require('aMap');
    // bmLocation = api.require('bmLocation');
    // console.log(JSON.stringify(map));
    app.get_data();
    app.get_user_data();
    // setTimeout(function () {
    //     app.getLocationLocal();
    //     // app.open();

    // }, 1000)
    //定时器（1秒钟加载一次）
    setInterval(app.getTime, 1000);

    // app.nowTime=moment().format('hh:mm:ss')
}